package com.codegeek.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 替换非法字符
 *
 * @author
 */
public class ReplaceIllegalChar {
    /**
     * 非法字符
     */
    private static final String ILLEGALITY_CHAR = "[\\*/:\\(\\)<>\\{}\\[\\]\\\\\\|%=\\+-]+";
    /**
     * 正则
     */
    private static final Pattern pattern = Pattern.compile(ILLEGALITY_CHAR);

    /**
     * 替换旧的字符串
     *
     * @param oldValue 原值
     * @return
     */
    public String replace(String oldValue) {
        StringBuffer newValue = new StringBuffer();
        Matcher matcher = pattern.matcher(oldValue);
        // 替换匹配到的正则
        while (matcher.find()) {
            matcher.appendReplacement(newValue, "");
        }
        // 匹配第一个正则
        matcher.appendTail(newValue);
        return newValue.toString();
    }
}
